<template>
  <div>
    <UploadExcel :before-upload="beforeUpload" :on-success="onSuccess" />
  </div>
</template>

<script>
import { importEmployees } from '@/api/employees'
import { formatExcelDate } from '@/utils'
export default {
  methods: {
    beforeUpload (file) {
      console.log(file)
      return true
    },
    async onSuccess ({ header, results }) {
      try {
        await importEmployees(this.transformResults(results))
        console.log('导入成功')
        this.$router.back()
      } catch (error) {
        console.log(error)
      }
    },
    transformResults (results) {
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '聘用形式': 'formOfEmployment',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName'
      }
      const newarr = []
      results.forEach(people => {
        const newpeo = {}
        for (const key in people) {
          const enkey = userRelations[key]
          if (enkey === 'timeOfEntry' || enkey === 'correctionTime') {
            newpeo[enkey] = formatExcelDate(people[key], '-')
          } else {
            newpeo[enkey] = people[key]
          }
        }
        newarr.push(newpeo)
      })

      return newarr
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
